package com.ruoyi.project.system.service.impl;

import java.util.List;

import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.project.system.domain.College;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.domain.UserNotice;
import com.ruoyi.project.system.mapper.CollegeMapper;
import com.ruoyi.project.system.mapper.UserNoticeMapper;
import com.ruoyi.project.system.service.IUserNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.framework.security.service.TokenService;

/**
 * 通知公告Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-10-07
 */
@Service
public class UserNoticeServiceImpl implements IUserNoticeService
{
    @Autowired
    private UserNoticeMapper userNoticeMapper;

    @Autowired
    private CollegeMapper collegeMapper;

    /**
     * 查询通知公告
     * 
     * @param id 通知公告ID
     * @return 通知公告
     */
    @Override
    public UserNotice selectUserNoticeById(Integer id)
    {
        return userNoticeMapper.selectUserNoticeById(id);
    }

    /**
     * 查询通知公告列表
     * 
     * @param userNotice 通知公告
     * @return 通知公告
     */
    @Override
    public List<UserNotice> selectUserNoticeList(UserNotice userNotice)
    {
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        if(loginUser != null){
            SysUser sysUser = loginUser.getUser();
            if(sysUser.getUserId() != 1) {
                Long collegeId = sysUser.getDeptId();
                userNotice.setCollegeId(collegeId);
                return userNoticeMapper.selectUserNoticeList(userNotice);
            }
        }

        return userNoticeMapper.selectUserNoticeListAdmin(userNotice);


    }

    /**
     * 新增通知公告
     * 
     * @param userNotice 通知公告
     * @return 结果
     */
    @Override
    public int insertUserNotice(UserNotice userNotice)
    {
        if(SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest()).getUser().getUserId()==1){
            userNotice.setTypeCode(1L);
            return userNoticeMapper.insertUserNotice(userNotice);
        }else{
            userNotice.setTypeCode(2L);
            Long collegeId = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest()).getUser().getDeptId();
            College college = collegeMapper.selectCollegeById(Integer.valueOf((int) collegeId.longValue()));
            if(SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest()).getUser().getDeptId()!=1){
                userNotice.setCollegeId(collegeId);
                userNotice.setCollegeCode(college.getCollegeCode());
                userNotice.setCollegeName(college.getCollegeName());
            }
            return userNoticeMapper.insertUserNotice(userNotice);

        }
    }

    /**
     * 修改通知公告
     * 
     * @param userNotice 通知公告
     * @return 结果
     */
    @Override
    public int updateUserNotice(UserNotice userNotice)
    {
        return userNoticeMapper.updateUserNotice(userNotice);
    }

    /**
     * 批量删除通知公告
     * 
     * @param ids 需要删除的通知公告ID
     * @return 结果
     */
    @Override
    public int deleteUserNoticeByIds(Integer[] ids)
    {
        return userNoticeMapper.deleteUserNoticeByIds(ids);
    }

    /**
     * 删除通知公告信息
     * 
     * @param id 通知公告ID
     * @return 结果
     */
    @Override
    public int deleteUserNoticeById(Integer id)
    {
        return userNoticeMapper.deleteUserNoticeById(id);
    }
}
